package com.alisls.shequtao.service.user.web;

import com.alisls.shequtao.common.core.exception.CommonException;
import com.alisls.shequtao.common.model.result.DataResult;
import com.alisls.shequtao.common.model.result.Result;
import com.alisls.shequtao.service.user.dto.TokenDTO;
import com.alisls.shequtao.service.user.dto.TokenInfoDTO;
import com.alisls.shequtao.service.user.dto.UserInfoDTO;
import com.alisls.shequtao.service.user.dto.UserPwdDTO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 登录管理
 *
 * @author Ke Wang
 */
@Api(tags = "登录管理")
@RestController
@RequestMapping("/user")
@Slf4j
public class LoginController {

    @Value("${oauth.token-url}")
    private String tokenUrl;

    @Resource
    private RestTemplate restTemplate;

    /**
     * 密码模式登录
     * @param userPwdDTO 用户名和密码
     * @return Token
     */
    @PostMapping("/login")
    public ResponseEntity<Result> login(@RequestHeader("Authorization") String authorization,
                                        @RequestBody UserPwdDTO userPwdDTO) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Authorization", authorization);

        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("grant_type", "password");
        params.add("username", userPwdDTO.getUsername());
        params.add("password", userPwdDTO.getPassword());

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, httpHeaders);
        try{
            ResponseEntity<TokenDTO> response = restTemplate.postForEntity(tokenUrl, requestEntity, TokenDTO.class);
            TokenDTO tokenDTO = response.getBody();
            log.info("用户名密码获取到Token {}", tokenDTO);
            TokenInfoDTO tokenInfoDTO = new TokenInfoDTO(tokenDTO.getAccess_token());
            return ResponseEntity.ok(DataResult.ofDataSuccess(tokenInfoDTO));
        } catch (Exception e) {
            e.printStackTrace();
        }

        throw new CommonException("登录失败！");
    }

    /**
     * 获取当前的用户信息
     * @return 用户信息
     */
    @GetMapping("/info")
    public ResponseEntity<Result> info() {
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        String[] roles = {"developer", "editor"};
        userInfoDTO.setRoles(roles);
        return ResponseEntity.ok(DataResult.ofDataSuccess(userInfoDTO));
    }

    /**
     * 用户登出
     * @return success
     */
    @GetMapping("/logout")
    public ResponseEntity<Result> logout() {
        return ResponseEntity.ok(DataResult.ofDataSuccess("ok"));
    }

}
